from django.shortcuts import render

from apps.api.mapper.sql_connect import read_user_info, read_data_from_db
from apps.api.service import data_process


# 登录界面
def login(request):
    return render(request, 'login_register.html')


# 可视化界面
def visualize(request):
    json_trend = data_process.deal_trend()
    base_info = data_process.deal_general()
    ride_info = data_process.deal_rank_ride()

    # 响应
    response = {
        'temperature_list': json_trend['temperature_list'],
        'RentedBikeCount': json_trend['RentedBikeCount'],
        'Rainfall': json_trend['Rainfall'],
        'Visibility': json_trend['Visibility'],
        'time': json_trend['time'],
        'total_num': base_info['total_num'],
        'holiday_percentage': base_info['holiday_percentage'],
        'over_rain': base_info['over_rain'],
        'over_wind': base_info['over_wind'],
        'monthly_ridership': ride_info['monthly_ridership'],
        'aver_vis_month': ride_info['aver_vis_month'],
        'aver_rain_month': ride_info['aver_rain_month'],
        'aver_rad_month': ride_info['aver_rad_month'],
        'month_list': ride_info['month_list'],
        'time_span_years': base_info['time_span_years'],
        'summer_count': base_info['summer_count'],
        'winter_count': base_info['winter_count'],
        'summer_riders': ride_info['summer_riders'],
        'spring_riders': ride_info['spring_riders'],
        'autumn_riders': ride_info['autumn_riders'],

    }
    return render(request, 'visualize.html', response)


# 用户管理界面
def user_backend(request):
    user_info = read_user_info()
    # 将 DataFrame 中的每条记录转换为元组
    records = [tuple(x) for x in user_info.itertuples(index=False)]
    columns_names = user_info.columns.tolist()
    response = {
        'data_info': records,
        'columns_names': columns_names
    }
    return render(request, 'backend.html', response)


# 数据管理界面
def data_manage(request):
    data = read_data_from_db().head(500)
    # 将 DataFrame 中的每条记录转换为元组
    records = [tuple(x) for x in data.itertuples(index=False)]
    columns_names = data.columns.tolist()
    response = {
        'data_info': records,
        'columns_names': columns_names
    }
    return render(request, 'info.html', response)
